<template>
  <div>
    <curd-title v-if="infoFlg" :title="'原料信息'" />
    <avue-crud
      :table-loading="materialLoading"
      :option="materialOption"
      :data="materialList"
      @on-load="onMaterialLoad"
      @row-save="rowMaterialSave"
      @row-update="rowMaterialUpdate"
      @refresh-change="refreshMaterialChange"
      @row-del="rowMaterialDel"
      :row-class-name="delRowClassName"
    >
      <template slot="mainMaterial" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.mainMaterialFlag === '1'">{{ scope.row.mainMaterial }}</del>
          {{ scope.row.mainMaterialA }}
        </div>
        <span v-else>{{ scope.row.mainMaterial }}</span>
      </template>
      <template slot="purchaseVolume" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.purchaseVolumeFlag === '1'">{{ scope.row.purchaseVolume }}</del>
          {{ scope.row.purchaseVolumeA }}
        </div>
        <span v-else>{{ scope.row.purchaseVolume }}</span>
      </template>
      <template slot="materialSupplier" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.materialSupplierFlag === '1'">{{ scope.row.materialSupplier }}</del>
          {{ scope.row.materialSupplierA }}
        </div>
        <span v-else>{{ scope.row.materialSupplier }}</span>
      </template>
      <template slot="cooperationYears" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.cooperationYearsFlag === '1'">{{ scope.row.cooperationYears }}</del>
          {{ scope.row.cooperationYearsA }}
        </div>
        <span v-else>{{ scope.row.cooperationYears }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { materialOption } from '@/option/tenant/supplier/factory-info'
import { getStore } from '@/util/store'
import { getMaterialList, removeMaterial, addMaterial, updateMaterial } from '@/api/supplier/material'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'MaterialInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      materialLoading: false,
      materialList: [],
      materialOption: materialOption.call(this)
    }
  },
  methods: {
    onMaterialLoad() {
      this.infoFlg
        ? ((this.materialLoading = true),
          getMaterialList({ supplierId: this.supplierId }).then(res => {
            this.materialList = res.data.data
            this.materialLoading = false
          }))
        : ''
    },
    refreshMaterialChange() {
      this.onMaterialLoad()
    },
    rowMaterialSave(row, done, loading) {
      addMaterial({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onMaterialLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowMaterialUpdate(row, index, done, loading) {
      updateMaterial(row).then(
        () => {
          this.onMaterialLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowMaterialDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeMaterial(row.id)
        })
        .then(() => {
          this.onMaterialLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
